<?php
namespace ZHMVC\DB\MANAGER;
if (! isset($_SESSION)) {
    session_start();
}
include (dirname(dirname(__FILE__)) . "/zhconfig/Config.php");
include (ZH_PATH . DS . MANAGERFPATH . DS . 'islogin' . ZH);

$isp = new \ZHMVC\D\MANAGER\isPermission();
$isper = $isp->getPermission();
$_curlid = $isp->getCUrl();
$c = new \ZHCONFIG\ZhConfig();
$db_pre = $c->getDbPre();


include (ZH_PATH . DS . MANAGERFPATH . DS . 'top1' . ZH);
?>
<div class="wap-container">
	<nav class="breadcrumb" style="background-color:#fff;padding: 0 24px">
	管理员管理
	<a class="btn btn-success radius f-r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a>
	</nav>
<?php 
$action=SafeRequest(getPGC('atcion'),0);
switch ($action)
{
    case "save":
		save($db_pre);
		break;
	case "del":
		del($db_pre);
		break;
	default:
		main($db_pre);
}

function main($db_pre)
{
    $tablename=SafeRequest(getPGC('tablename'),0);
?>
<article class="Hui-admin-content clearfix">
<form action="?atcion=save&tablename=<?php echo $tablename; ?>" id="form1" name="form1" method="post" class="form form-horizontal">

<div class="panel mt-20">
	<div class="panel-body">
		<div class="clearfix mt-20">
            <table class="table table-striped table-bordered">
              <thead>
                <tr>
                    <th>列名</th>
                	<th>是否显示</th>
                	<th>排序</th>
                  </tr>
              </thead>
              <tbody>
  <?php
  $rs = new \ZHMVC\D\ShowTables();
  $s = $rs->getColumns($tablename);
  for ($i = 0, $imax = count($s); $i < $imax; $i ++) {
      $liename="";
      if($s[$i]['comment']!="")
      {
          $liename=$s[$i]['comment'];
      }
      else 
      {
          $liename=$s[$i]['field'];
      }
  ?>
  <tr> 
    <td><?php echo $liename;?></td>
    <td>
		<?php 
		$sql="select id,isshow,xuhao from zhmvc_yctable where `datatable`='".$tablename."' and liename='".$s[$i]['field']."'";
		$data = $rs->getSqlOne($sql);
		$rows = $rs->getRowCount();
		//echo $rows;
		if($rows>0)
		{
		    //已经存在了
		    if($data['isshow']==1)
		    {
		        //显示
		        echo "<div class=\"skin-minimal\">
                        <div class=\"check-box\">
                            <label>
                                <input type=\"checkbox\" id=\"selected".$i."\" 
                                    name=\"admin_power['".$s[$i]['field']."']\" value=\"1\" 
                                    checked>
                                                                                      显示
                            </label>
                        </div>
					</div>";
		        
		    }
		    else{
		        //隐藏
		        echo "<div class=\"skin-minimal\">
                        <div class=\"check-box\">
                            <label>
                                <input type=\"checkbox\" id=\"selected".$i."\" 
                                    name=\"admin_power['".$s[$i]['field']."']\" value=\"0\" 
                                    >
                                                                                      显示
                            </label>
                        </div>
					</div>";
		    }
		}
		else
		{
		    $sql1="insert into zhmvc_yctable(`datatable`,`liename`,`colindex`,`lietagname`,`isshow`) values ('".$tablename."','".$s[$i]['field']."','0','".$s[$i]['comment']."','1')";
		    $rs->SqlUpdate($sql1);
		    echo "<div class=\"skin-minimal\">
                        <div class=\"check-box\">
                            <label>
                                <input type=\"checkbox\" id=\"selected".$i."\"
                                    name=\"admin_power['".$s[$i]['field']."']\" value=\"1\"
                                    checked>
                                                                                      显示
                            </label>
                        </div>
					</div>";
		}
		?>
	</td>
	<td>
		<input type="text" id="xuhao_<?php echo $s[$i]['field'];?>" name="xuhao[<?php echo $s[$i]['field'];?>]" class="input-text" placeholder=""
        value="<?php echo $data['xuhao']; ?>" />
	</td>
  </tr>
<?php
    }
?>
              </tbody>
            </table>
					</div>
				</div>
			</div>
			<div class="row clearfix">
					<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
						<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
					</div>
			</div>
</form>
</article>
  
<?php 
}

function save()
{
    
    $xuhao=$_POST['xuhao'];
    //var_dump($xuhao);
	$lie=$_POST['admin_power'];
	$tablename=getPGC('tablename');
	$rs = new \ZHMVC\D\ShowTables();
	$sql="select * from zhmvc_yctable where `datatable`='".$tablename."'";
	$datas = $rs->getSqlAll($sql);
	$rows = $rs->getRowCount();
	//echo $rows;
	if($rows>0)
	{
	    for($i=0;$i<$rows;$i++)
	    {
	        $data=$datas[$i];
	        $id=$data['id'];
	        $liename="'".$data['liename']."'";
	        $newxuhao=$xuhao[$data['liename']];
	        
	        if($newxuhao=="") $newxuhao=0;
	        if(in_array($liename, array_keys($lie))) {
	            // key已设置
	            //echo $liename."已设置<br>";
	            $sql1="update zhmvc_yctable set isshow='1',xuhao='".$newxuhao."' where id=".$id;
	            //echo $sql1."<br>";
	            $rs->SqlUpdate($sql1);
	        } else {
	            // key未设置
	           //echo $liename."未设置<br>";
	           $sql1="update zhmvc_yctable set isshow='0',xuhao='".$newxuhao."' where id=".$id;
	           //echo $sql1."<br>";
	           $rs->SqlUpdate($sql1);
	        } 
	    }
	}
	//echo "<script>alert('更新成功');window.location.href='admin_label.php?tablename=".$tablename."';</script>";
?>
</div>
<?php 
include (ZH_PATH . DS . MANAGERFPATH . DS . "foot1" . ZH);
?>
<script type="text/javascript">
function layer_close(){
    parent.tableshuaxin();
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}
layer_close();
</script>
</body>
</html>
<?php
}

function del()
{
	$postid=SafeRequest(getPGC('id'),0);

	if(($postid!="") && ($postid!="0"))
	{
        $wheremap = array("id" => $postid);
        
        D("zhmvc_label")->where($wheremap)->LinkDelete($wheremap);
		echo "已经删除。<br>";
	}
}

?>
</div>
<?php 
include (ZH_PATH . DS . MANAGERFPATH . DS . 'foot1' . ZH);
?>
	<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
